package com.wlc.thread;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wanglichao
 * @date 2022-02-17 14:39:20
 * @description 程序倒计时器
 */
public class CountDownLatchDemo2 {

    /***
     * 10个固定线程数的线程池
     */
    public void method02() {
        // 10个并行任务
        int size = 3;
        /***
         * 初始化批处理工具
         */
        BatchHandleExecutorUtil batchHandleExecutor = BatchHandleExecutorUtil.init(size, Executors.newFixedThreadPool(10));
        // 添加统计任务
        batchHandleExecutor.execute(() -> {
            // 加入要处理的任务1
            System.out.println("test1");
        });
        batchHandleExecutor.execute(() -> {
            // 加入要处理的任务2
            System.out.println("test2");
        });
        // 加入任务
        batchHandleExecutor.execute(() -> {
            // 加入要处理的任务10
            System.out.println("test3");
        });
        // 等待所有任务执行完成
        batchHandleExecutor.await();
        System.out.println("结束");
    }


    public static void method01() throws InterruptedException {
        List<String> list = Arrays.asList("总舵主，陈近南", "莲花堂香主，蔡德忠",
                "洪顺堂香主，方大洪",
                "家后堂香主，马超兴",
                "参太堂香主，胡德帝",
                "宏化堂香主，李式开",
                "青木堂香主，韦小宝",
                "赤火堂香主，古至中",
                "玄水堂香主，林永超",
                "黄土堂香主，姚必达");

        CountDownLatch latch = new CountDownLatch(10);
        ExecutorService exec = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            int idx = i;
            exec.execute(() -> {
                try {
                    System.out.println("天地会核心十堂核心成员，高层会议，成员：" + list.get(idx) + " 入场");
                    Thread.sleep(3000);
                } catch (Exception ignore) {
                } finally {
                    latch.countDown();
                }
            });
        }
        latch.await();
        System.out.println("天地会，核心成员到齐。开会讨论谁入宫，偷取四十二章经！「内定韦香主」");
        exec.shutdown();
    }

    public static void main(String[] args) throws InterruptedException {
        method01();
    }


}
